package com.ff.xyh.service.complaint.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ff.xyh.common.entity.complaint.ComplaintHandle;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.qo.complaint.ComplaintInfoQuery;
import com.ff.xyh.common.entity.complaint.ComplaintInfo;
import com.ff.xyh.service.complaint.mapper.ComplaintInfoMapper;
import com.ff.xyh.service.complaint.service.ComplaintHandleService;
import com.ff.xyh.service.complaint.service.ComplaintInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 投诉信息表 服务实现类
 * </p>
 *
 * @author ff
 * @since 2023-05-24
 */
@Service
public class ComplaintInfoServiceImpl extends ServiceImpl<ComplaintInfoMapper, ComplaintInfo> implements ComplaintInfoService {

    @Autowired
    ComplaintHandleService complaintHandleService;

    @Override
    public ResultObject getComplaintInfoCondition(ComplaintInfoQuery complaintInfoQuery) {

        Page<ComplaintInfo> page = new Page<>(complaintInfoQuery.getCurrentPage(), complaintInfoQuery.getPageSize());
        List<ComplaintInfo> resultList = new ArrayList<>();
        QueryWrapper<ComplaintInfo> wrapper = new QueryWrapper<>();
        // 如果通过id查询 那就只需要查询id 然后直接封装返回
        if (!StringUtils.isEmpty(complaintInfoQuery.getId())) {
            ComplaintInfo getById = this.getById(complaintInfoQuery.getId());
            if (getById != null)
                resultList.add(getById);
            return ResultObject.ok().putList(resultList, resultList.size());
        }

        if (!StringUtils.isEmpty(complaintInfoQuery.getTextContain())) {
            wrapper.like("complaint_text", complaintInfoQuery.getTextContain());
        }

        if (complaintInfoQuery.getGmtAfter() != null) {
            wrapper.ge("gmt_created", complaintInfoQuery.getGmtAfter());
        }
        if (complaintInfoQuery.getGmtBefore() != null) {
            wrapper.le("gmt_created", complaintInfoQuery.getGmtBefore());
        }
        // 按创建时间排序意味着要最新
        if (complaintInfoQuery.getOrderByCreatedTime() != null && complaintInfoQuery.getOrderByCreatedTime()) {
            // 降序
            wrapper.orderByDesc("gmt_created");
        } else {
            wrapper.orderByAsc("gmt_created");
        }


        if (complaintInfoQuery.getComplaintType() != null) {
            wrapper.eq("complaint_type", complaintInfoQuery.getComplaintType());
        }
        if (complaintInfoQuery.getStatus() != null) {
            wrapper.eq("status", complaintInfoQuery.getStatus());
        }



        IPage<ComplaintInfo> resultPage = this.page(page, wrapper);

        return ResultObject.ok().putList(resultPage.getRecords(), resultPage.getTotal());

    }

    @Override
    public ResultObject handleComplaintInfo(ComplaintHandle complaintHandle) {
        if (complaintHandle.getStatus() < 1)
            return ResultObject.error().message(ResultMessage.ERROR_ARG);

        ComplaintInfo byId = this.getById(complaintHandle.getComplaintId());
        byId.setStatus(complaintHandle.getStatus());
//        byId.setGmtModified(null);
        if (this.updateById(byId)) {
            if (complaintHandleService.save(complaintHandle)) {
                return ResultObject.ok().putData(complaintHandle);
            } else {
                return ResultObject.error().message(ResultMessage.FAILED_TO_UPDATE);
            }
        } else {
            return ResultObject.error().message(ResultMessage.FAILED_TO_UPDATE);
        }

    }
}
